package com.wangzhenyu.learn.test.kotlin.skill

@Suppress("UNUSED")
object InstanceOfSkill {

    //let
    //public inline fun <T, R> T.let(block: (T) -> R): R
    fun let() {
        val book = Book().let {
            it.name = "《计算机网络》"
            "This book is ${it.name}"
        }
        print(book)
    }

    //run
    //public inline fun <T, R> T.run(block: T.() -> R): R
    fun run01() {
        Book().run {
            name = "《计算机网络》"
            price = 30
            displayInfo()
        }
    }

    //run
    //public inline fun <R> run(block: () -> R): R
    fun run02() {
        val temp = kotlin.run {
            "sss"
        }
        println(temp)
    }

    //with
    //public inline fun <T, R> with(receiver: T, block: T.() -> R): R
    fun with() {
        val book = Book()
        with(book) {
            name = "《计算机网络》"
            price = 40
        }
        print(book)
    }

    //apply
    //public inline fun <T> T.apply(block: T.() -> Unit): T
    fun apply() {
        val book: Book = Book().apply {
            name = "《计算机网络》"
            price = 40
        }
        print(book)
    }

    //also
    //public inline fun <T> T.also(block: (T) -> Unit): T
    fun also() {
        val book: Book = Book().also {
            it.name = "《计算机网络》"
            it.price = 40
        }
        print(book)
    }


}

class Book {
    var name = "《数据结构》"
    var price = 60
    fun displayInfo() = println("Book name : $name and price : $price")
}